package com.lanlian.cayman.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.lanlian.commonlib.bean.CityInfo;
import com.lanlian.commonlib.bean.ProvinceInfo;
import com.lanlian.commonlib.bean.RespLazyEnum;
import com.lanlian.cayman.R;
import com.lanlian.uikit.OnWheelChangedListener;
import com.lanlian.uikit.WheelView;
import com.lanlian.utillib.CommonUtils;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * CityPickerLayout
 * put two WheelView into  LinearLayout
 */
public class CityPickerLayout extends LinearLayout {

    private WheelView mProvincePicker;
    private WheelView mCityPicker;

    private int mCurrProvinceIndex = -1;

    private RespLazyEnum mRespUtilEnum;
    private ArrayList<CityInfo> mCityList = new ArrayList<>();
    private ArrayList<ProvinceInfo> mProvinceList = new ArrayList<>();

    public CityPickerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CityPickerLayout(Context context) {
        this(context, null);
    }

    public void setAreaInfo(ArrayList<ProvinceInfo> provinceList, RespLazyEnum respUtilEnum) {
        this.mProvinceList = provinceList;
        this.mRespUtilEnum = respUtilEnum;

        ArrayList<String> provinces = new ArrayList<>();
        for (ProvinceInfo provinceInfo : mProvinceList) {
            provinces.add(provinceInfo.getName());
        }
        mProvincePicker.setEntries(provinces);
        mProvincePicker.setCurrentIndex(0);

        mCityList = getCityByProvince(provinceList.get(0));
        mCityPicker.setEntries(getCityStringByProvince(provinceList.get(0)));
        mCityPicker.setCurrentIndex(0);

        mProvincePicker.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView view, int oldIndex, int newIndex) {
                if (mCurrProvinceIndex != newIndex) {
                    mCurrProvinceIndex = newIndex;
                    String selectProvince = (String) mProvincePicker.getCurrentItem();
                    if (TextUtils.isEmpty(selectProvince)) {
                        return;
                    }
                    mCityList = getCityByProvince(mProvinceList.get(mCurrProvinceIndex));
                    ArrayList<String> city = getCityStringByProvince(mProvinceList.get(mCurrProvinceIndex));
                    if (city.size() == 0) {
                        return;
                    }
                    mCityPicker.setEntries(city);
                    mCityPicker.setCurrentIndex(0);
                }
            }
        });
    }

    private ArrayList<String> getCityStringByProvince(ProvinceInfo provinceInfo) {
        ArrayList<String> list = new ArrayList<>();
        for (CityInfo cityInfo : provinceInfo.getCityList()) {
            list.add(cityInfo.getName());
        }
        return list;
    }

    private ArrayList<CityInfo> getCityByProvince(ProvinceInfo provinceInfo) {
        ArrayList<CityInfo> cityList = new ArrayList<>();
        if (CommonUtils.isListEmpty(provinceInfo.getCityList())) {
            for (TreeMap.Entry<String, String> entry : mRespUtilEnum.getCity().entrySet()) {
                if (entry.getKey().startsWith(provinceInfo.getId().substring(0, 2))) {
                    CityInfo cityInfo = new CityInfo();
                    cityInfo.setId(entry.getKey());
                    cityInfo.setName(entry.getValue());
                    cityList.add(cityInfo);
                }
            }
            provinceInfo.setCityList(cityList);
        } else {
            cityList = provinceInfo.getCityList();
        }
        return cityList;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.lay_city_picker, this);
        mProvincePicker = findViewById(R.id.province_wv);
        mCityPicker = findViewById(R.id.city_wv);
    }

    public ProvinceInfo getProvince() {
        if (mProvincePicker.getCurrentIndex() >= 0) {
            if (mProvincePicker == null) {
                return null;
            }
            return mProvinceList.get(mProvincePicker.getCurrentIndex());
        }
        return null;
    }

    public CityInfo getCity() {
        if (mCityPicker.getCurrentIndex() >= 0) {
            if (mCityPicker == null) {
                return null;
            }
            return mCityList.get(mCityPicker.getCurrentIndex());
        }
        return null;
    }

    public ProvinceInfo defaultProvince(String provinceId) {
        ProvinceInfo province = null;
        for (ProvinceInfo provinceInfo : mProvinceList) {
            if (provinceInfo.getId().equals(provinceId)) {
                province = provinceInfo;
                mProvincePicker.setCurrentIndex(mProvinceList.indexOf(provinceInfo));
                break;
            }
        }
        return province;
    }

    public CityInfo defaultCity(String cityId) {
        CityInfo city = null;
        for (CityInfo cityInfo : mCityList) {
            if (cityInfo.getId().equals(cityId)) {
                city = cityInfo;
                mCityPicker.setCurrentIndex(mCityList.indexOf(cityInfo));
                break;
            }
        }
        return city;
    }

}
